////////////////////////////////////////////////////////////
// Workfile : RSACrypt.cpp
// Author : Andreas Lehner
// Date : 12.11.2012
// Description : class RSACrypt: implementation of class RSACrypt
// -> crypting with RSA algorithm
// Remarks : -
// Revision : 1
////////////////////////////////////////////////////////////
#include "RSACrypt.h"

unsigned char RSACrypt::calcRSA(unsigned char number, int const& exponent){
	unsigned int product = 1;
	unsigned char toCrypt;
	//exponentiate number to get crypted key
	for (int i = 0; i < exponent; i++){
		product *= number;
		//modulo operation is done in every loop to stay in int range
		//characteristic of modulo: proportion for exponentiating stays right
		product = product % mN;
	}
	toCrypt = product;
	return toCrypt;
}

void RSACrypt::Encrypt(){
	unsigned char rsa;
	//iterate through string and encrypt every character
	for (size_t i = 0; i < mData.size(); ++i){
		rsa = calcRSA(mData[i], mE);
		mData[i] = rsa;
	}
}

void RSACrypt::Decrypt(){
	unsigned char rsa;
	//iterate through string and decrypt every character
	for (size_t i = 0; i < mData.size(); ++i){
		rsa = calcRSA(mData[i], mD);
		mData[i] = rsa;
	}
}

//return file extension for RSA crypting
std::string RSACrypt::getFileExtension() const{
	return ".RSA";
}
